<?php
class Nhapxe extends Controller {
	private $data;
	
	function index() {		
		$this -> loadTools();
		$this -> loadData();
		$this -> load -> view('nhapxe/view_loaixe',$this -> data );
	}

	function loadTools() {
		$this -> load -> helper('form');
		$this -> load -> database();
		
	}
	
	function loadData() {
		$query = $this -> db -> get('xemay_category');


		$this -> data['title'] = 'Nhap xe: Chon loai xe';
		//Get the options
		$this -> data['options'] = array ('Chon xe...');

		foreach ($query -> result() as $row ) {
			array_push($this -> data['options'],$row -> TEN .'/'. $row -> MAU);
			
		}

		$this -> data['gia_nhap'] = array (
			'name' => 'gia_nhap',
			'style' => 'width:200',
			'value' => '',
			'id' => 'gia_nhap'
			);

			$this -> data['so_luong']  = array (
			'name' => 'so_luong',
			'style' => 'width:200',
			'onchange' => 'sayHello(this)'
			);

			//TODO: load it automatic from session (stored in session by login time)
			$this -> data['current_user'] = 'lan';

	}

	function printSellPrice($index) {
		$this -> load -> model('Xemay');		
		echo $this -> Xemay -> getBuyPriceWithId($index);
	}

	
	function addXemay() {
		//LOAD helpers
		$this -> load -> model('Xemay_items');
		$this -> load -> model('Xemay');
		$this -> load -> helper('my_utilities');
		$this -> load -> helper('url');
		
		$choosenItem = $_POST['choosenItem'];
		$gia_nhap = $_POST['gia_nhap'];
		$so_luong = $_POST['so_luong'];
		$so_khung = $_POST['so_khung'];
		$so_may = $_POST['so_may'];
		
		//TODO: get user from session
		$nguoi_nhap = 'lan';

		$xemay_name = getNameFromDesc($choosenItem);
		$color = getColorFromDesc($choosenItem);
		
		$catid = $this -> Xemay -> getIdFromDesc($xemay_name,$color);	
		$this -> Xemay_items -> insertItems(
			$catid,
			$so_luong,
			$so_khung,$so_may,
			$gia_nhap,$nguoi_nhap 
		);
		
		redirect('nhapxe/nhapxe');	
	}
}
?>
